package com.lwl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lwl.entity.MyPage;
import com.lwl.entity.TRole;
import com.lwl.entity.Tuser;
import com.lwl.service.TRoleService;
import com.lwl.dao.TRoleMapper;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * (TRole)表服务实现类
 *
 * @author Lei
 * @since 2022-10-22 16:38:50
 */
@Service
public  class TRoleServiceImpl implements TRoleService {
   @Autowired
   private TRoleMapper tRoleDao;

   @Override
   public List<TRole> getAll() {
      System.out.println("正在查询用户信息");
      return tRoleDao.selectAll();
   }

   @Override
   public PageInfo queryAllRole(MyPage myPage, Tuser tuser) {
      PageHelper.startPage(myPage.getPage(), myPage.getLimit());
      List<TRole> tRoles = tRoleDao.selectAll();
      PageInfo pageInfo = new PageInfo(tRoles);
      return pageInfo;
   }

   @Override
   public boolean insertRole(TRole tRole) {
         int insert = -1;
         if (tRole.getId()==null){
            tRole.setCreatetime(new Date());
            tRole.setUpdatetime(new Date());
            insert = tRoleDao.insert(tRole);
         }else {
            tRole.setUpdatetime(new Date());
            insert = tRoleDao.updateByPrimaryKeySelective(tRole);
         }
      return insert>0?true:false;
   }

   @Override
   public boolean queryByRoleName(String rname) {
      Example example = new Example(TRole.class);
      example.createCriteria().andEqualTo("rname",rname);
      TRole tRole1 = tRoleDao.selectOneByExample(example);
      if (tRole1==null){
         return true;
      }else {
         return false;
      }

   }

   @Override
   public boolean deleteRole(Integer id) {
      List<TRole> tusers = tRoleDao.selectUserById(id);
      if (tusers.size()==0){
         int i = tRoleDao.deleteByPrimaryKey(id);
         return true;
      }else {
         return false;
      }

   }
}
